Strict
Import mojo
Import agfx


Class TGUISlider


	Private
	Field _value:Float 
	Field length:Float
	Field vec_A:TVector2 = New TVector2()
	Field vec_B:TVector2 = New TVector2()

	Public
	Field x:Int
	Field y:Int
	Field width:Int
	Field height:Int
	Field min:Float
	Field max:Float
	Field value:Float

	Field sprite:TSprite
	Field sl_left:TImage
	Field sl_middle:TImage
	Field sl_right:TImage

	Method New(name:String,img_sl_left:String,img_sl_middle:String,img_sl_right:String,img_sl_button:String,x:Int,y:Int,width:Int,height:Int,min:Float,max:Float,value:Float)
	
		Self.x 			= x
		Self.y 			= y
		Self.width 		= width
		Self.height 	= height
		Self.min 		= min
		Self.max 		= max
		Self.value		= value
		
		Self.vec_A.Set(Self.min,0)
		Self.vec_B.Set(Self.max,0)
		Self.length = Self.vec_A.DistanceTo(Self.vec_B)

		Self.sprite = New TSprite(name,IMAGES.Find(img_sl_button),self.x,self.y)
		Self.sl_left = IMAGES.Find(img_sl_left)
		Self.sl_middle = IMAGES.Find(img_sl_middle)
		Self.sl_right = IMAGES.Find(img_sl_right)
		
	End	


	Method Update:Void()

		If Self.IsMouseInside() And MOUSE.Down()Then
			Self.sprite.position.x = MOUSE.x
			
			Self._value = MOUSE.x - self.x
			Self.value = Self.min + (Self._value * ((Self.max - Self.min) / float(Self.width)))
			
			Self.vec_B.Set(Self._value,0)
			Local part:FLoat = Self.vec_A.DistanceTo(Self.vec_B)

		End
		Self.sprite.position.x = Self.x + (Self.value - Self.min) / ((Self.max - Self.min) / Float(Self.width))
		Self.sprite.Update()
	End

	Method Render:Void()
		
		SetColor(255,255,255)
		
		Self.sl_left.Draw(Self.x,Self.y)
		Self.sl_middle.Draw(Self.x+Self.width/2.0,Self.y,0,Self.width/Self.sl_middle.w,1)
		Self.sl_right.Draw(Self.x+Self.width,Self.y)
		Self.sprite.Draw()

		DrawText("VALUE: "+self.value,10,0)
	End

	Method IsMouseInside:Bool()
		Local res:Bool=False

		if MOUSE.x >= Self.x And
			MOUSE.x <= Self.x + Self.width And
			MOUSE.y >= Self.y - 4 And
			MOUSE.y <= Self.y + Self.height + 4 Then res =True

		Return res

	End

End
